package handler

import (
	"github.com/gin-gonic/gin"
	"pic-text-mgmt/model"
	"strconv"
)

func Release(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Login{
		Username: username,
		Password: password,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var user model.Login
	model.DB.Offset(offset).First(size).Find(&user)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": user,
	})
	return
}
